package br.inf.solutions.apresentacoes.ssp.model.reports;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import br.com.vexillum.util.ReflectionUtils;
import br.com.vexillum.util.SpringFactory;
import br.inf.solutions.apresentacoes.ssp.control.bulletin.BulletinTopicsController;
import br.inf.solutions.apresentacoes.ssp.model.bulletin.BulletinItem;
import br.inf.solutions.apresentacoes.ssp.model.bulletin.BulletinTopics;
import br.inf.solutions.apresentacoes.ssp.model.bulletin.items.BehaviorChange;
import br.inf.solutions.apresentacoes.ssp.model.bulletin.items.DispenseFunctionPerson;
import br.inf.solutions.apresentacoes.ssp.model.bulletin.items.FunctionPerson;
import br.inf.solutions.apresentacoes.ssp.model.bulletin.items.Promotion;
import br.inf.solutions.apresentacoes.ssp.model.bulletin.items.Removal;
import br.inf.solutions.apresentacoes.ssp.model.bulletin.items.TransferReserve;
import br.inf.solutions.apresentacoes.ssp.model.bulletin.items.TransferUnity;

public class BulletinReport {

	private List<Removal> listRemoval;
	private List<DispenseFunctionPerson> listDispenseFunctionPerson;
	private List<FunctionPerson> listFunctionPerson;
	private List<Promotion> listPromotion;
	private List<TransferUnity> listTransferUnity;
	private List<TransferReserve> listTransferReserve;
	private List<BehaviorChange> listBehaviorChange;

	public BulletinReport() {
		initLists(null);
	}
	
	public BulletinReport(Long idBulletin) {
		initLists(idBulletin);
	}
	
//	private void initLists(Long idBulletin){
//		listRemoval = createListRemovals(idBulletin);
//		listDispenseFunctionPerson = createListDispensesFunctionPerson(idBulletin);
//		listFunctionPerson = createListFunctionsPerson(idBulletin);
//		listPromotion = createListPromotions(idBulletin);
//		listTransferUnity = createListTranfersUnitys(idBulletin);
//		listTransferReserve = createListTransfersReserve(idBulletin);
//		listBehaviorChange = createListBehaviorChange(idBulletin);
//	}
	
	@SuppressWarnings("unchecked")
	private void initLists(Long idBulletin){
		BulletinTopicsController controllerTopics = SpringFactory.getController("bulletinTopicsController",	BulletinTopicsController.class, null);
		List<BulletinTopics> list = (List<BulletinTopics>) controllerTopics.doAction("listAll", false).getList();
		for (BulletinTopics topic : list) {
			String nameItem= topic.getTableName();
			try {
				Class<?> clazz = Class.forName("br.inf.solutions.apresentacoes.ssp.model.bulletin.items." + nameItem);
				BulletinItem item =  (BulletinItem) clazz.newInstance();
				for (Field field : ReflectionUtils.getFields(this.getClass())) {
					field.setAccessible(true);
					if(field.getName().equals("list" + nameItem)){
						field.set(this, item.getControl().getNotPublicatedItens(idBulletin));
						break;
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
				continue;
			}
		}
	}
	
//	private List<Removal> createListRemovals(Long idBulletin){
//		return new Removal().getControl().getNotPublicatedItens();
//	}
//	
//	private List<DispenseFunctionPerson> createListDispensesFunctionPerson(Long idBulletin){
//		return (List<DispenseFunctionPerson>) new DispenseFunctionPerson().getControl().getNotPublicatedItens();
//	}
//	
//	private List<FunctionPerson> createListFunctionsPerson(Long idBulletin){
//		return new FunctionPerson().getControl().getNotPublicatedItens();
//	}
//	
//	private List<Promotion> createListPromotions(Long idBulletin){
//		return new Promotion().getControl().getNotPublicatedItens();
//	}
//	
//	private List<TransferUnity> createListTranfersUnitys(Long idBulletin){
//		return new TransferUnity().getControl().getNotPublicatedItens();
//	}
//	
//	private List<TransferReserve> createListTransfersReserve(Long idBulletin){
//		return new TransferReserve().getControl().getNotPublicatedItens();
//	}
//	
//	private List<BehaviorChange> createListBehaviorChange(Long idBulletin){
//		return new BehaviorChange().getControl().getNotPublicatedItens();
//	}
	
	public List<Removal> getListRemoval() {
		return listRemoval;
	}

	public void setListRemoval(List<Removal> listRemoval) {
		this.listRemoval = listRemoval;
	}

	public List<DispenseFunctionPerson> getListDispenseFunctionPerson() {
		return listDispenseFunctionPerson;
	}

	public void setListDispenseFunctionPerson(
			List<DispenseFunctionPerson> listDispenseFunctionPerson) {
		this.listDispenseFunctionPerson = listDispenseFunctionPerson;
	}

	public List<FunctionPerson> getListFunctionPerson() {
		return listFunctionPerson;
	}

	public void setListFunctionPerson(List<FunctionPerson> listFunctionPerson) {
		this.listFunctionPerson = listFunctionPerson;
	}

	public List<Promotion> getListPromotion() {
		return listPromotion;
	}

	public void setListPromotion(List<Promotion> listPromotion) {
		this.listPromotion = listPromotion;
	}

	public List<TransferUnity> getListTransferUnity() {
		return listTransferUnity;
	}

	public void setListTransferUnity(List<TransferUnity> listTransferUnity) {
		this.listTransferUnity = listTransferUnity;
	}

	public List<TransferReserve> getListTransferReserve() {
		return listTransferReserve;
	}

	public void setListTransferReserve(List<TransferReserve> listTransferReserve) {
		this.listTransferReserve = listTransferReserve;
	}

	public List<BehaviorChange> getListBehaviorChange() {
		return listBehaviorChange;
	}

	public void setListBehaviorChange(List<BehaviorChange> listBehaviorChange) {
		this.listBehaviorChange = listBehaviorChange;
	}

	public static List<?> testList(){
		List<BulletinReport> teste = new ArrayList<>();
		teste.add(new BulletinReport());
		return teste;
	}

}
